package com.lazycat.backend.service.impl.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lazycat.backend.mapper.AddressMapper;
import com.lazycat.backend.mapper.GoodMapper;
import com.lazycat.backend.mapper.OrderMapper;
import com.lazycat.backend.mapper.UserMapper;
import com.lazycat.backend.pojo.Address;
import com.lazycat.backend.pojo.Good;
import com.lazycat.backend.pojo.Orders;
import com.lazycat.backend.pojo.User;
import com.lazycat.backend.service.user.FetchUserOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FetchUserOrdersServiceImpl implements FetchUserOrdersService {

    @Autowired
    private GoodMapper goodMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressMapper addressMapper;


    @Override
    public JSONObject fetch(Integer userId) {

        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        JSONObject resp = new JSONObject();
        ArrayList<JSONObject> items = new ArrayList<>();
        ordersQueryWrapper.eq("buyer_id", userId);
        List<Orders> orders = orderMapper.selectList(ordersQueryWrapper);
        for (Orders order : orders) {
            JSONObject item = new JSONObject();
            item.put("buyer_id", order.getBuyerId());
            item.put("seller_id", order.getBuyerId());
            item.put("time_stamp", order.getTimeStamp());
            item.put("good_id", order.getGoodId());

            QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
            Good good = goodMapper.selectOne(goodQueryWrapper.eq("id", order.getGoodId()));
            item.put("good_name", good.getGoodName());

            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            User buyer = userMapper.selectOne(userQueryWrapper.eq("id", order.getBuyerId()));
            item.put("buyer_name", buyer.getUsername());

            QueryWrapper<User> _userQueryWrapper = new QueryWrapper<>();
            User seller = userMapper.selectOne(_userQueryWrapper.eq("id", order.getSellerId()));
            item.put("seller_name", seller.getUsername());

            // 加入地址
            QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
            Address address = addressMapper.selectOne(addressQueryWrapper.eq("address_id", order.getAddressId()));
            JSONObject addr = new JSONObject();
            addr.put("address_id", address.getAddressId());
            addr.put("tag", address.getTag());
            addr.put("address", address.getAddress());
            addr.put("contact", address.getContact());
            addr.put("phone_number", address.getPhoneNumber());

            item.put("address", addr);

            items.add(item);
        }

        resp.put("orders", items);
        return resp;

    }
}
